﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NetworkPower.Common;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.Windows.Forms;
using NetworkPower.DataModel.Models;

namespace NetworkPower.UI
{
    /// <summary>
    /// Interaction logic for ProjectForm.xaml
    /// </summary>
    /// 


    public partial class ProjectForm : Window
    {
        NetworkPowerContext context = new NetworkPowerContext();
        private Project CurrentProject { get; set; }

        public ProjectForm()
        {
            InitializeComponent();

            LoadProjects();
        }

        private void LoadProjects()
        {
            lbProject.ItemsSource = context.Projects.ToList();
            lbProject.DisplayMemberPath = "ProjectName";
        }

        private void CreateProgram_Click(object sender, RoutedEventArgs e)
        {
            var form = new CreateProject();
            form.ShowDialog();
            form.Close();
            LoadProjects();

        }

        private void EnterProgram_Click(object sender, RoutedEventArgs e)
        {
            if (lbProject.SelectedItems.Count == 1)
            {
                using (var form = new ComparePort(((Project)(lbProject.SelectedItems[0]))))
                {
                    form.ShowDialog();
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("请选择工程！");
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            for (int i = 0; i < lbProject.Items.Count; i++)
            {
                Project pi = lbProject.Items[i] as Project;
                if (pi.ProjectName.ToString().ToUpper().Contains(txtSearch.Text.ToUpper()))
                {
                    lbProject.SelectedIndex = i;
                }
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (lbProject.SelectedItems.Count == 1)
            {
                EditProject editproject = new EditProject(((Project)(lbProject.SelectedItems[0])));
                editproject.ShowDialog();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("请选择工程！");
            }
        }

        private void btnDefineDevice_Click(object sender, RoutedEventArgs e)
        {
            if (lbProject.SelectedItems.Count == 1)
            {
                DefineDevice defineDevice = new DefineDevice(((Project)(lbProject.SelectedItems[0])));
                defineDevice.ShowDialog();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("请选择工程！");
            }
        }
    }
}
